package com.campuscarbooking.api.dto.response;

import com.campuscarbooking.api.model.Reservation;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReservationResponse {
    private Long id;
    private String status;
    private String paymentStatus;
    private String paymentMethod;
    private String seatNumber;
    private BigDecimal amountPaid;
    private LocalDateTime reservationTime;
    private Boolean signedIn;
    private LocalDateTime signInTime;
    private LocalDateTime canceledAt;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    private ScheduleInfo schedule;
    private UserInfo user;

    public static ReservationResponse fromReservation(Reservation reservation) {
        ScheduleInfo scheduleInfo = null;
        if (reservation.getSchedule() != null) {
            scheduleInfo = ScheduleInfo.builder()
                    .id(reservation.getSchedule().getId())
                    .departureTime(reservation.getSchedule().getDepartureTime())
                    .arrivalTime(reservation.getSchedule().getArrivalTime())
                    .routeDirection(reservation.getSchedule().getRouteDirection().name())
                    .price(reservation.getSchedule().getPrice())
                    .build();
        }

        UserInfo userInfo = null;
        if (reservation.getUser() != null) {
            userInfo = UserInfo.builder()
                    .id(reservation.getUser().getId())
                    .nickname(reservation.getUser().getNickname())
                    .phone(reservation.getUser().getPhone())
                    .userType(reservation.getUser().getUserType().name())
                    .build();
        }

        return ReservationResponse.builder()
                .id(reservation.getId())
                .status(reservation.getStatus().name())
                .paymentStatus(reservation.getPaymentStatus().name())
                .paymentMethod(reservation.getPaymentMethod() != null ?
                        reservation.getPaymentMethod().name() : null)
                .seatNumber(reservation.getSeatNumber())
                .amountPaid(reservation.getAmountPaid())
                .reservationTime(reservation.getReservationTime())
                .signedIn(reservation.getSignedIn())
                .signInTime(reservation.getSignInTime())
                .canceledAt(reservation.getCanceledAt())
                .createdAt(reservation.getCreatedAt())
                .updatedAt(reservation.getUpdatedAt())
                .schedule(scheduleInfo)
                .user(userInfo)
                .build();
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ScheduleInfo {
        private Long id;
        private LocalTime departureTime;
        private LocalTime arrivalTime;
        private String routeDirection;
        private BigDecimal price;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class UserInfo {
        private Long id;
        private String nickname;
        private String phone;
        private String userType;
    }
} 